package com.matol.entity;

import java.io.Serializable;

import org.springframework.stereotype.Repository;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.matol.util.Tools;

/** 
 *  
 * @author matol 
 * @date 2013-01-22 ����23:34 
 */  
@Repository(value = "user") 
public class User implements Validator ,Serializable {

	private Integer id;
	private String name;
	private String pass;
	private Integer age;
	
	public User(){}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	
	public boolean supports(Class<?> clazz) {
        return clazz.equals(User.class);
    }
	public void validate(Object target, Errors errors) {
		User userVO = (User)target;  
        ValidationUtils.rejectIfEmpty(errors, "name", "name_required", "name的内容不能为空");  
        ValidationUtils.rejectIfEmpty(errors, "pass", "pass_required", "pass的内容不能为空"); 
        ValidationUtils.rejectIfEmpty(errors, "age", "age_required", "age的内容不能为空"); 
        
        int length = userVO.getName().length();  
        if (length < 2) {  
            errors.rejectValue("name", "name_too_short", "用户名不能小于{2}个字符");  
        }        
        if (length > 20) {  
            errors.rejectValue("name", "name_too_long", "用户名不能超过{20}个字符");  
        }
        
        length = userVO.getPass().length();  
        if (length < 2) {  
            errors.rejectValue("pass", "pass_too_short", "密码太短，不能少于{2}个字符");  
        }
        if (length > 20) {  
            errors.rejectValue("pass", "pass_too_long", "密码太长，不能长于{20}个字符");  
        }
        
        if(userVO.getAge() == null || Tools.isNumber(userVO.getAge().toString()) == 0){
        	errors.rejectValue("age", "age_number", "年龄必须为数字");  
        }else{
            if (Tools.isNumber(userVO.getAge().toString()) < 10) {  
                errors.rejectValue("age", "age_too_short", "年龄太小，不能少于{10}岁");  
            }
            if (Tools.isNumber(userVO.getAge().toString()) > 100) {  
                errors.rejectValue("age", "age_too_long", "年龄太大，不能长于{100}岁");  
            }
        }
         
    }  
	
	
	
}
